package com.milkshopbackend.milkshop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.milkshopbackend.milkshop.Service.ProductSearchService;
import com.milkshopbackend.milkshop.Service.ProductService;
import com.milkshopbackend.milkshop.common.Result;
import com.milkshopbackend.milkshop.entity.Product;
import com.milkshopbackend.milkshop.entity.ProductDTO;
import com.milkshopbackend.milkshop.entity.ProductSearchDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;

@RestController
@RequestMapping("/api/products")
@RequiredArgsConstructor
public class ProductController {

    private final ProductService productService;
    private final ProductSearchService productSearchService;

    @PostMapping
    public Product createProduct(@Valid @RequestBody ProductDTO productDTO) {
        return productService.createProduct(productDTO);
    }

    @PutMapping("/{id}")
    public Product updateProduct(@PathVariable Long id, @Valid @RequestBody ProductDTO productDTO) {
        return productService.updateProduct(id, productDTO);
    }

    @DeleteMapping("/{id}")
    public void deleteProduct(@PathVariable Long id) {
        productService.deleteProduct(id);
    }

    @GetMapping("/{id}")
    public Result<Product> getProductDetail(@PathVariable Long id) {
        Product product = productService.getProduct(id);
        return Result.success(product);
    }

    @GetMapping
    public Result<IPage<ProductDTO>> listProducts(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "50") Integer size,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) BigDecimal minPrice,
            @RequestParam(required = false) BigDecimal maxPrice,
            @RequestParam(required = false) Integer minRating,
            @RequestParam(required = false) String sortField,
            @RequestParam(required = false) String sortOrder) {

        ProductSearchDTO searchDTO = new ProductSearchDTO();
        searchDTO.setPageNum(page);
        searchDTO.setPageSize(size);
        searchDTO.setCategoryId(categoryId);
        searchDTO.setKeyword(keyword);
        searchDTO.setMinPrice(minPrice);
        searchDTO.setMaxPrice(maxPrice);
        searchDTO.setMinRating(minRating);
        searchDTO.setSortField(sortField);
        searchDTO.setSortOrder(sortOrder);

        return Result.success(productSearchService.search(searchDTO));
    }

    @PutMapping("/{id}/status")
    public void updateStatus(@PathVariable Long id, @RequestParam boolean status) {
        productService.updateStatus(id, status);
    }

    @GetMapping("/page")
    public Result<IPage<ProductDTO>> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) BigDecimal minPrice,
            @RequestParam(required = false) BigDecimal maxPrice,
            @RequestParam(required = false) Integer minRating,
            @RequestParam(required = false) String sortField,
            @RequestParam(required = false) String sortOrder) {

        ProductSearchDTO searchDTO = new ProductSearchDTO();
        searchDTO.setPageNum(pageNum);
        searchDTO.setPageSize(pageSize);
        searchDTO.setCategoryId(categoryId);
        searchDTO.setKeyword(keyword);
        searchDTO.setMinPrice(minPrice);
        searchDTO.setMaxPrice(maxPrice);
        searchDTO.setMinRating(minRating);
        searchDTO.setSortField(sortField);
        searchDTO.setSortOrder(sortOrder);

        return Result.success(productSearchService.search(searchDTO));
    }

}
